/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package com.aionemu.gameserver.model.gameobjects.stats.modifiers;

import java.util.List;

import com.aionemu.gameserver.dataholders.DataManager;
import com.aionemu.gameserver.model.gameobjects.Item;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.gameobjects.stats.Stat;
import com.aionemu.gameserver.model.templates.item.ItemTemplate;

/**
 * @author Mr. Poke
 *
 */
public class DuplicateStatModifier extends StatModifier
{

	/* (non-Javadoc)
	 * @see com.aionemu.gameserver.model.gameobjects.stats.modifiers.StatModifier#apply(com.aionemu.gameserver.model.gameobjects.stats.Stat)
	 */
	@Override
	public void apply(Stat stat)
	{
		Item mainWeapon = ((Player) stat.getOwner()).getEquipment().getMainHandWeapon();
		Item offWeapon = ((Player) stat.getOwner()).getEquipment().getOffHandWeapon();
		if (mainWeapon != null)
		{
			SimpleModifier modifier1 = null;
			SimpleModifier modifier2 = null;
			List<StatModifier> modifiers = mainWeapon.getItemTemplate().getModifiers();
			if (modifiers != null)
			{
				for (StatModifier modifier : modifiers)
				{
					if (modifier.getStat() == this.getStat())
					{
						modifier1 = (SimpleModifier) modifier;
					}
				}
			}

			if (mainWeapon.hasFusionedItem())
			{
				ItemTemplate template = DataManager.ITEM_DATA.getItemTemplate(mainWeapon.getFusionedItem());
				if (template != null && template.getModifiers() != null)
				{
					for (StatModifier modifier : template.getModifiers())
					{
						if (modifier.getStat() == this.getStat())
						{
							modifier2 = (SimpleModifier) modifier;
						}
					}
				}
			}
			else if (offWeapon != null)
			{
				List<StatModifier> modifiers2 = offWeapon.getItemTemplate().getModifiers();
				if (modifiers2 != null)
				{
					for (StatModifier modifier : modifiers2)
					{
						if (modifier.getStat() == this.getStat())
						{
							modifier2 = (SimpleModifier) modifier;
						}
					}
				}
			}

			if (modifier2 == null && modifier1 == null)
				return;
			else if (modifier2 != null && modifier1 == null)
			{
				modifier2.apply(stat);
			}
			else if (modifier2 == null && modifier1 != null)
			{
				modifier1.apply(stat);
			}
			else
			{
				if (Math.abs(modifier1.getValue()) >= Math.abs(modifier2.getValue()))
					modifier1.apply(stat);
				else
					modifier2.apply(stat);
			}
		}
	}

}
